﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class TradeHistoryNoteMarker : UserControl
    {

        #region Private Variables

            // The date that the note was added
          
            // The date that the note is placed on
            DateTime datePlaced;

            // Offset from stock chart center
            double xOffsetFromChartCenter;
            double yOffsetFromChartCenter;

            // Indicates whether mouse is over read note popup dialog
            bool isHoveringOverReadDialog = false;

            private readonly string defaultEditMessage;

            Popup noteReadPopup;
            TradeHistoryNoteReadDialog readDialog;

            Popup noteEditPopup;
            TradeHistoryNoteEditDialog editDialog;

        #endregion

        #region Constructors

            public TradeHistoryNoteMarker(DateTime dateAdded, DateTime datePlaced, double xOffset, double yOffset)
                : this(dateAdded, datePlaced)
            {
                XOffset = xOffset;
                YOffset = yOffset;             
            }

            public TradeHistoryNoteMarker(DateTime dateAdded, DateTime datePlaced)
            {
                InitializeComponent();

                defaultEditMessage = string.Format((string)Resources["Res_EditMessage"], Environment.NewLine);

                this.datePlaced = datePlaced;

                noteReadPopup = new Popup();
                readDialog = new TradeHistoryNoteReadDialog();

                noteEditPopup = new Popup();
                editDialog = new TradeHistoryNoteEditDialog();
                editDialog.editNoteDelete += new EventHandler(DeleteButton_MouseLeftButtonUp);
                editDialog.editNoteSave += new EventHandler(SaveButton_MouseLeftButtonUp);
                editDialog.GotFocus += new RoutedEventHandler(EditNoteTextBox_GotFocus);

                noteReadPopup.Child = readDialog;
                noteEditPopup.Child = editDialog;

                // Hide popups
                
                //ToggleEditNoteDialog(true);
                //ToggleReadNoteDialog(false);
                noteReadPopup.IsOpen = false;
                noteEditPopup.IsOpen = true;

                // Set date added
                //ReadNoteDate.Text = "added " + dateAdded.ToString("MM/dd/yyyy");
                readDialog.DateCreated = string.Format((string)Resources["Res_DateCreated"], dateAdded.ToShortDateString()); //ToString("MM/dd/yyyy"));

                // Set edit popup default text
                //EditNoteTextBox.Text = defaultEditMessage;
                editDialog.DialogText = defaultEditMessage;
            }

          



        #endregion

        #region Public Methods

            /// <summary>
            /// Close note read and note edit canvas
            /// </summary>
            public void ClosePopups()
            {
                //Note_Read.Visibility = Visibility.Collapsed;
                //Note_Edit.Visibility = Visibility.Collapsed;
                //ToggleReadNoteDialog(false);
                //ToggleEditNoteDialog(false);
                noteReadPopup.IsOpen = false;
                noteEditPopup.IsOpen = false;
            }

        #endregion

        #region Helper Methods
            /*
            private void ToggleEditNoteDialog(bool show)
            {
                if (show)
                {
                    EditNoteScaleXKeyFrame.Value = 1;
                    EditNoteScaleYKeyFrame.Value = 1;
                    EditNoteLeftKeyFrame.Value = xOffsetFromChartCenter;
                    EditNoteTopKeyFrame.Value = yOffsetFromChartCenter;                    
                }
                else
                {
                    EditNoteScaleXKeyFrame.Value = 0;
                    EditNoteScaleYKeyFrame.Value = 0;
                    EditNoteLeftKeyFrame.Value = 0;
                    EditNoteTopKeyFrame.Value = 0;                    
                }
                ToggleEditNoteDialogAnimation.Begin();
            }

            private void ToggleReadNoteDialog(bool show)
            {
                if (show)
                {
                    ReadNoteScaleXKeyFrame.Value = 1;
                    ReadNoteScaleYKeyFrame.Value = 1;
                    ReadNoteLeftKeyFrame.Value = xOffsetFromChartCenter;
                    ReadNoteTopKeyFrame.Value = yOffsetFromChartCenter;
                }
                else
                {
                    ReadNoteScaleXKeyFrame.Value = 0;
                    ReadNoteScaleYKeyFrame.Value = 0;
                    ReadNoteLeftKeyFrame.Value = 0;
                    ReadNoteTopKeyFrame.Value = 0;
                }
                ToggleReadNoteDialogAnimation.Begin();
            }
        */
        #endregion

        #region Get/Set Properties

            /// <summary>
            /// Get or set the date that this date is placed on
            /// according to the chart
            /// </summary>
            public DateTime DatePlaced
            {
                get { return datePlaced; }
                set { datePlaced = value; }
            }

            public double XOffset
            {
                get { return xOffsetFromChartCenter; }
                set 
                { 
                    xOffsetFromChartCenter = value;
                    //Note_Read.SetValue(Canvas.LeftProperty, xOffsetFromChartCenter);
                    //Note_Edit.SetValue(Canvas.LeftProperty, xOffsetFromChartCenter);
                    //noteReadPopup.SetValue(Canvas.LeftProperty, xOffsetFromChartCenter);
                    //noteEditPopup.SetValue(Canvas.LeftProperty, xOffsetFromChartCenter);
                    noteReadPopup.HorizontalOffset = xOffsetFromChartCenter;
                    noteEditPopup.HorizontalOffset = xOffsetFromChartCenter;

                }
            }

            public double YOffset
            {
                get { return yOffsetFromChartCenter; }
                set 
                {                     
                    yOffsetFromChartCenter = value; 
                    //Note_Read.SetValue(Canvas.TopProperty, yOffsetFromChartCenter);
                    //Note_Edit.SetValue(Canvas.TopProperty, yOffsetFromChartCenter);
                    //noteReadPopup.SetValue(Canvas.TopProperty, yOffsetFromChartCenter);
                    //noteEditPopup.SetValue(Canvas.TopProperty, yOffsetFromChartCenter);
                    noteReadPopup.VerticalOffset = yOffsetFromChartCenter;
                    noteEditPopup.VerticalOffset = yOffsetFromChartCenter;

                }
            }
        
        #endregion

        #region Event Handlers

            /// <summary>
            /// Popup up the read note
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void NoteBubble_MouseEnter(object sender, MouseEventArgs e)
            {
                // Make sure we`re not editing the note before popping up
                //if (ScaleEditNoteDialog.ScaleX == 0)
                if(noteEditPopup.IsOpen == false)
                {                    
                    //ToggleReadNoteDialog(true);
                    noteReadPopup.IsOpen = true;

                    // Tell stock chart to raise z order
                    if (noteEnter != null)
                    {
                        noteEnter(this, e);
                    }
                }
            }

            /// <summary>
            /// Hide the read note
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void NoteBubble_MouseLeave(object sender, MouseEventArgs e)
            {
                //if (!isHoveringOverReadDialog && ScaleEditNoteDialog.ScaleX == 0)
                if (!isHoveringOverReadDialog && noteEditPopup.IsOpen == false)
                {                    
                    //ToggleReadNoteDialog(false);
                    noteReadPopup.IsOpen = false;

                    // Tell stock chart to reset z order
                    //if (noteLeave != null && ScaleEditNoteDialog.ScaleX == 0)
                    if (noteLeave != null && noteEditPopup.IsOpen == false)
                    {
                        noteLeave(this, e);
                    }
                }
            }

            /// <summary>
            /// Show the edit note
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void NoteBubble_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {                
                //ToggleReadNoteDialog(false);
                //ToggleEditNoteDialog(true);
                noteReadPopup.IsOpen = false;
                noteEditPopup.IsOpen = true;
                editDialog.focusOn();

                // Tell stock chart to raise z order
                if (noteEnter != null)
                {
                    noteEnter(this, e);
                }
            }

            /// <summary>
            /// User clicks check indicator in upper right hand corner
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void SaveButton_MouseLeftButtonUp(object sender, EventArgs e)
            {
                //ToggleEditNoteDialog(false);
                //ToggleReadNoteDialog(false);
                noteEditPopup.IsOpen = false;
                noteReadPopup.IsOpen = false;
                
                //ReadNoteText.Text = EditNoteTextBox.Text;
                readDialog.DialogText = editDialog.DialogText;

                // Tell stock chart to reset z order
                if (noteLeave != null)
                {
                    //noteLeave(this, e);
                }
            }

            /// <summary>
            /// User clicks "X" indicator in upper right hand corner
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void DeleteButton_MouseLeftButtonUp(object sender,EventArgs e)
            {
                // Fake a delete by just collapsing it
                Root.Visibility = Visibility.Collapsed;

                // Close edit dialog popup
                noteEditPopup.IsOpen = false;

                // Tell stock chart to reset z order
                if (noteLeave != null)
                {
                    //noteLeave(this, e);
                }
            }

            private void Note_Read_MouseLeave(object sender, MouseEventArgs e)
            {
                //if (ReadNoteDialogScale.ScaleX == 1)
                if (noteReadPopup.IsOpen)
                {
                    isHoveringOverReadDialog = false;
                    
                    //ToggleReadNoteDialog(false);
                    noteReadPopup.IsOpen = false;

                    // Tell stock chart to reset z order
                    if (noteLeave != null)
                    {
                        noteLeave(this, e);
                    }
                }
            }

            private void Note_Read_MouseEnter(object sender, MouseEventArgs e)
            {
                isHoveringOverReadDialog = true;
            }

            /// <summary>
            /// Edit note textbox got focus
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void EditNoteTextBox_GotFocus(object sender, RoutedEventArgs e)
            {/*
                if (EditNoteTextBox.Text == defaultEditMessage)
                {
                    EditNoteTextBox.Text = "";
                }
              */
                if (editDialog.DialogText == defaultEditMessage)
                {
                    editDialog.DialogText = "";
                }
            }

          

            /// <summary>
            /// Edit note text has changed
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void EditNoteTextBox_KeyDown(object sender, KeyEventArgs e)
            {
                /*
                string text = EditNoteTextBox.Text;
                string newText = "";
                int oldindex = 0;

                // If it is an empty string, then return
                if (text.Length == 0)
                {
                    return;
                }

                do
                {
                    int i = text.IndexOf("\r");

                    if (i == -1)
                    {
                        i = text.Length;
                    }
 
                    if (i > 23)
                    {
                        newText += text.Substring(0, 24) + "\r" + text.Substring(24, text.Length + 1 - i);
                    }
                    else
                    {
                        newText += text.Substring(0, i);
                    }

                    text = text.Substring(i, text.Length - i);

                } while (text.Length < 0);

                EditNoteTextBox.Text = newText;
                EditNoteTextBox.Select(EditNoteTextBox.Text.Length, 0);
                 */
            }

        #endregion

        #region Events

            public event MouseEventHandler noteEnter;
            public event MouseEventHandler noteLeave;

        #endregion
         
    }
}
